﻿using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace WebAPI.Filter
{
    /// <summary>
    /// AllowMultiple 同一个地方是否可以多次使用
    /// Inherited 子类是否继承该标签
    /// </summary>
    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method,AllowMultiple = false, Inherited = true)]
    public class UnitOfWorkAttribute:Attribute
    {
        public Type[] DbContextTypes { get; init;}
        public UnitOfWorkAttribute(params Type[] dbContextTypes) 
        {
            this.DbContextTypes = dbContextTypes;
            foreach (var type in dbContextTypes)
            {
                if (!typeof(DbContext).IsAssignableFrom(type)) //判断Type 必须为DbContext类型
                {
                    throw new ArgumentException($"{type} must inherit from DbContext");
                }
            }
        }
    }
}
